<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/2/26 0026
 * Time: 9:39
 */
namespace app\admin\controller;
use app\admin\model\Subject;
use think\Controller;
use think\Db;

class NewBuilder extends Controller
{
    private $type = 'form';
    private $header = [
        'alldel'=>[
            'show'=>false
        ],
        'add'=>[
            'show'=>false
        ],
        'back'=>[
            'show'=>false
        ],
        'sousuo'=>[
            'show'=>false
        ],
        'text'=>[
            'show'=>false
        ],
        'refresh'=>[
            'show'=>false
        ]
    ];
    private $column = [];
    private $right_header = [];
    private $right=[];
    private $page=[];
    private $action = '';
    private $form = [];
    private $select = [];
    private $button = [];
    /**创建类型
     * @param string $type
     * @return $this
     */
    static public function make($type='form'){
        $obj = new NewBuilder();
        if($type){
            $obj->type = $type;
        }
        return $obj;
    }

    /**添加头部全删除按钮
     * @param string删除接口 $action
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addTableHeaderAllDel($action='',$desc='全部删除',$class='',$style=''){
        $array['show'] =  true;
        $array['action'] = url($action);
        $array['class'] = $class;
        $array['style'] = $style;
        $array['desc'] = $desc;
        $this->header['alldel'] = $array;
        return $this;
    }

    /**添加头部添加按钮
     * @param string $action
     * @param string $class
     * @param string $style
     * @return $this
     */
    public function addTableHeaderAdd($action='',$desc='添加',$class='',$style=''){
        $array['show'] =  true;
        $array['action'] = url($action);
        $array['class'] = $class;
        $array['style'] = $style;
        $array['desc'] = $desc;
        $this->header['add'] = $array;
        return $this;
    }

    /**添加头部返回按钮
     * @param string返回的地址 $action
     * @param string文字 $desc
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addTableHeaderBack($action='',$desc='返回',$class='',$style=''){
        $array['show'] =  true;
        if(!$action){
            $array['action'] = '';
        }else{
            $array['action'] = url($action);
        }
        $array['class'] = $class;
        $array['style'] = $style;
        $array['desc'] = $desc;
        $this->header['back'] = $array;
        return $this;
    }

    /**添加头部下拉框条件
     * @param string字段名 $name
     * @param string描述 $desc
     * @param array选项 $option
     * @param string提交地址 $action
     * @return $this
     */
    public function addTableSelect($name='',$desc='',$option=[],$action=''){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['option'] = $option;
        $array['action'] = url($action);
        $this->select[] = $array;
        return $this;
    }

    /**添加条件搜索框
     * @param string条件搜索 $action
     * @param string条件 $tiaojain
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addTableHeaderSousuo($tiaojain='',$desc='搜索',$class='',$style=''){
        $array['show'] =  true;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['desc'] = $desc;
        $array['tiaojian'] = $tiaojain;
        $this->header['sousuo'] = $array;
        return $this;
    }

    /**添加头部文字
     * @param string $desc
     * @param string $class
     * @param string $style
     * @return $this
     */
    public function addTableHeaderText($desc='',$class='',$style=''){
        $array['show'] =  true;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['desc'] = $desc;
        $this->header['text'] = $array;
        return $this;
    }

    /**添加一列
     * @param字段名 $name
     * @param string类型 $type
     * @param string描述 $desc
     * @param string样式 $class
     * @param string样式 $style
     * @param array可选项 $option
     * @return $this
     */
    public function addTableColumn($name,$type='text',$desc='',$option=['启用','禁用'],$action='',$width='',$style='text-align: center',$align='center'){
        $array['name'] = $name;
        $array['type'] = $type;
        $array['desc'] = $desc;
        $array['style'] = $style;
        $array['width'] = $width;
        $array['action'] = url($action);
        $array['option'] = $option;
        $array['align'] = $align;
        $this->column[] = $array;
        return $this;
    }

    /**添加表格右侧头部
     * @param string $desc
     * @param string $class
     * @param string $style
     */
    public function addTableRightHeader($desc='',$class='',$style='',$width=null){
        $array['desc'] = $desc;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['show'] = true;
        $array['width'] = $width;
        $this->right_header = $array;
        return $this;
    }

    /**添加表格右侧按钮
     * @param string类型 $type
     * @param string接口地址 $action
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addTableRight($type='button',$action='',$desc='',$class='',$style=''){
        $array['type'] = $type;
        $array['action'] = url($action);
        $array['style'] = $style;
        $array['desc'] = $desc;
        $array['class'] = $class;
        $this->right[] = $array;
        return $this;
    }

    /**添加分页
     * @param string分页接口 $action
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addTablePage($action='',$class='',$style='',$show=true){
//        $array['type'] = $type;
        $array['action'] = url($action);
        $array['style'] = $style;
        $array['class'] = $class;
        $array['show'] = $show;
        $this->page = $array;
        return $this;
    }

    /**form表单添加按钮
     * @param string按钮类型 $type
     * @param string添加 $desc
     * @return $this
     */
    public function addFormButton($type='submit',$desc='提交'){
        $array['type'] = $type;
        $array['desc'] = $desc;
        $this->button[] = $array;
        return $this;
    }
    /**添加input框
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addInput($name='',$desc='',$value='',$class='',$style='',$readonly = false){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['type'] = 'input';
        $array['value'] = $value;
        $array['readonly'] = $readonly;
        $this->form[] = $array;
        return $this;
    }

    /**添加密码框
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addPwd($name='',$desc='',$value='',$class='',$style=''){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['type'] = 'pwd';
        $array['value'] = $value;
        $this->form[] = $array;
        return $this;
    }

    /**添加隐藏域
     * @param string字段名 $name
     * @param string值 $value
     * @return $this
     */
    public function addNone($name='',$value=''){
        $array['name'] = $name;
        $array['value'] = $value;
        $array['type'] = 'none';
        $this->form[] = $array;
        return $this;
    }

    /**添加时间域
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @param bool是否显示时分秒 $ymdhis
     * @return $this
     */
    public function addTime($name='',$desc='',$value='',$class='',$style='',$ymdhis=false){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['type'] = 'time';
        if($ymdhis){
            if($value){
                $array['value'] = date('Y-m-d H:i:s',$value);
            }else{
                $array['value'] = date('Y-m-d H:i:s',time());
            }

        }else{
            if($value){
                $array['value'] = date('Y-m-d',$value);
            }else{
                $array['value'] = date('Y-m-d',time());
            }
        }
        $this->form[] = $array;
        return $this;
    }

    /**添加多行文本
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addTextarea($name='',$desc='',$value='',$class='',$style='',$readonly=false){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['class'] = $class;
        $array['value'] = $value;
        $array['style'] = $style;
        $array['readonly'] = $readonly;
        $array['type'] = 'textarea';
        $this->form[] = $array;
        return $this;
    }

    /**添加单选框
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param array选项 $option
     * @param string样式 $class
     * @param string样式 $style
     * @param bool是否key对应value $keytostr
     * @return $this
     */
    public function addRadio($name='',$desc='',$value='',$option=[],$class='',$style='',$keytostr=false){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['class'] = $class;
        $array['value'] = $value;
        $array['option'] = $option;
        $array['keytostr'] = $keytostr;
        $array['style'] = $style;
        $array['type'] = 'radio';
        $this->form[] = $array;
        return $this;
    }

    /**添加多选框
     * @param string字段名 $name
     * @param string描述 $desc
     * @param array值 $value
     * @param array选项 $option
     * @param string样式 $class
     * @param string样式 $style
     * @param bool是否key对应value $keytostr
     * @return $this
     */
    public function addCheckbox($name='',$desc='',$value=[],$option=[],$class='',$style='',$keytostr=false){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['class'] = $class;
        $array['value'] = $value;
        $array['option'] = $option;
        $array['keytostr'] = $keytostr;
        $array['style'] = $style;
        $array['type'] = 'checkbox';
        $this->form[] = $array;
        return $this;
    }

    /**添加开关
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addSwitch($name='',$desc='',$value=1,$class='',$style=''){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['class'] = $class;
        $array['value'] = $value;
        $array['style'] = $style;
        $array['type'] = 'switch';
        $this->form[] = $array;
        return $this;
    }

    /**添加单文件
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addFile($name='',$desc='',$value='',$class='',$style=''){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['value'] = $value;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['type'] = 'file';
        $this->form[] = $array;
        return $this;
    }

    /**添加多文件
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addFiles($name='',$desc='',$value='',$class='',$style=''){
        $values = '';
        if($value){
            $values = explode(',',$value);
        }
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['value'] = $value;
        $array['values'] = $values;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['type'] = 'files';
        $this->form[] = $array;
        return $this;
    }

    /**添加制度文本信息
     * @param 描述 $desc
     * @param string值 $value
     * @return $this
     */
    public function addText($desc,$value=''){
        $array['type'] = 'text';
        $array['desc'] = $desc;
        $array['value'] = $value;
        $this->form[] = $array;
        return $this;
    }
    /**添加单图片
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addImg($name='',$desc='',$value='',$class='',$style=''){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['value'] = $value;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['type'] = 'img';
        $this->form[] = $array;
        return $this;
    }

    /**添加多图片
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addImgs($name='',$desc='',$value='',$class='',$style=''){
        $values = '';
        if($value){
            $values = explode(',',trim($value,','));
        }
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['value'] = $value;
        $array['values'] = $values;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['type'] = 'imgs';
        $this->form[] = $array;
        return $this;
    }

    /**添加下拉框
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param array选项 $option
     * @param string样式 $class
     * @param string样式 $style
     * @param bool是否key对应值 $keytostr
     * @return $this
     */
    /**
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param array选项 $option
     * @param bool是否key对应值 $keytostr
     * @param string $class
     * @param string $style
     * @return $this
     */
    public function addSelect($name='',$desc='',$value='',$option=[],$keytostr=false,$class='',$style=''){
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['value'] = $value;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['keytostr'] = $keytostr;
        $array['option'] = $option;
        $array['type'] = 'select';
        $this->form[] = $array;
        return $this;
    }

    /**添加编辑框
     * @param string字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param string样式 $class
     * @param string样式 $style
     * @return $this
     */
    public function addEditor($name='',$desc='',$value='',$class='',$style=''){

        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['value'] = $value;
        $array['class'] = $class;
        $array['style'] = $style;
        $array['type'] = 'editor';
        $this->form[] = $array;
        $this->editor[] = $array;
        return $this;
    }

    /**添加联动下拉框
     * @param 字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param null模型id $model_id
     * @param int层级 $ceng
     * @param null样式 $class
     * @param null样式 $style
     * @return $this|bool
     */
    public function addDropDown($name,$desc='',$value='',$table=null,$ceng=1,$action='',$class=null,$style=null){
        $option1 = [];
        $option2 = [];
        $option3 = [];
        $pid1 = 0;
        $pid2 = 0;
        if($value){
            $id   = $value;
            $data = Db::name($table)->where('id',$id)
                ->where('delete_time',null)
                ->find();
//            echo '<pre>';
//            print_r($data);
//            exit;
            if($data){
                $option3 = Db::name($table)->where('pid',$data['pid'])
                    ->where('delete_time',null)
                    ->select();
                $pid2 = $data['pid'];
                $data = Db::name($table)->where('id',$pid2)->find();
                if($data){
                    $pid1 = $data['pid'];
                    $option2 = Db::name($table)
                        ->where('pid',$pid1)
                        ->where('delete_time',null)
                        ->select();
                    $data = Db::name($table)
                        ->where('id',$pid1)
                        ->where('delete_time',null)
                        ->find();
                    if($data){
                        $option1 = Db::name($table)
                            ->where('pid',$data['pid'])
                            ->where('delete_time',null)
                            ->select();
                    }
                }
            }
        }else{
            $option1 = Db::name($table)->where('pid',0)->select();
        }
//        echo '<pre>';
//        print_r($option3);
//        print_r($option2);
//        print_r($option1);
//        exit;
        $array['name'] = $name;
        $array['option1'] = $option1;
        $array['option2'] = $option2;
        $array['option3'] = $option3;
        $array['pid1'] = $pid1;
        $array['pid2'] = $pid2;
        $array['name'] = $name;
        $array['name'] = $name;
        $array['desc'] = $desc;
        $array['ceng'] = $ceng;
        $array['type'] = 'dropdown';
        $array['class'] = $class;
        $array['style'] = $style;
        $array['table'] = $table;
        $array['action'] = url($action);
        $array['value'] = $value;
        $this->form[] = $array;
        return $this;
    }

    /**添加提交接口
     * @param $str
     * @return $this
     */
    public function addAction($str){
        $this->action = url($str);
        return $this;
    }
    /**加载
     * @param $cid
     * @param null $id
     * @param string $action
     * @return mixed
     */
    public function getForm($cid,$id=null,$action='document/add',$edit=true,$pid=null){
        $obj = new Subject();
        $obj->setCid($cid);
        $lanmu = $obj->getColumn();
        $model = $obj->getModel();
        $field = $obj->getField();
        $data = [];
        if($id){
            $data = Db::name($model['model_table'])->where('id',$id)->find();
        }
        $builder = $this->addAction($action.'?cid='.$cid);
        if($data){
            if($edit){
                $builder = $builder->addNone('id',$data['id']);
            }
        }
        $builder = $builder->addNone('cid',$cid);
        $builder = $builder->addText('栏目',$lanmu['name']);
        foreach ($field as $value){
            if($value['fields']=='cid'||$value['fields']=='update_time'){
                continue;
            }
            if($value['form_type']=='单行文本'){
                if($value['fields']=='sort'){
                    if($data){
                        $builder = $builder->addInput($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addInput($value['fields'],$value['field_name'],100);
                    }
                }else{
                    if($data){
                        $builder = $builder->addInput($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addInput($value['fields'],$value['field_name']);
                    }
                }
            }
            if($value['form_type']=='多行文本'){
                if($data){
                    $builder = $builder->addTextarea($value['fields'],$value['field_name'],$data[$value['fields']]);
                }else{
                    $builder = $builder->addTextarea($value['fields'],$value['field_name']);
                }
            }
            if($value['form_type']=='单文件'){
                if($data){
                    $builder = $builder->addFile($value['fields'],$value['field_name'],$data[$value['fields']]);
                }else{
                    $builder = $builder->addFile($value['fields'],$value['field_name']);
                }
            }
            if($value['form_type']=='多文件'){
                if($data){
                    $builder = $builder->addFiles($value['fields'],$value['field_name'],$data[$value['fields']]);
                }else{
                    $builder = $builder->addFiles($value['fields'],$value['field_name']);
                }
            }
            if($value['form_type']=='单图片'){
                if($data){
                    $builder = $builder->addImg($value['fields'],$value['field_name'],$data[$value['fields']]);
                }else{
                    $builder = $builder->addImg($value['fields'],$value['field_name']);
                }
            }
            if($value['form_type']=='多图片'){
                if($data){
                    $builder = $builder->addImgs($value['fields'],$value['field_name'],$data[$value['fields']]);
                }else{
                    $builder = $builder->addImgs($value['fields'],$value['field_name']);
                }
            }
            if($value['form_type']=='下拉框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addSelect($value['fields'],$value['field_name'],$data[$value['fields']],$option);
                }else{
                    $builder = $builder->addSelect($value['fields'],$value['field_name'],null,$option);
                }
            }
            if($value['form_type']=='单选框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addRadio($value['fields'],$value['field_name'],$data[$value['fields']],$option);
                }else{
                    $builder = $builder->addRadio($value['fields'],$value['field_name'],null,$option);
                }
            }
            if($value['form_type']=='多选框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $da = explode(',',trim($data[$value['fields']]));
                    $builder = $builder->addCheckbox($value['fields'],$value['field_name'],$da,$option);
                }else{
                    $builder = $builder->addCheckbox($value['fields'],$value['field_name'],null,$option);
                }
            }
            if($value['form_type']=='编辑框'){
                if($data){
                    $builder = $builder->addEditor($value['fields'],$value['field_name'],$data[$value['fields']]);
                }else{
                    $builder = $builder->addEditor($value['fields'],$value['field_name']);
                }
            }
            if($value['form_type']=='开关'){
                if($data){
                    $builder = $builder->addSwitch($value['fields'],$value['field_name'],$data[$value['fields']]);
                }else{
                    $builder = $builder->addSwitch($value['fields'],$value['field_name']);
                }
            }
            if($value['form_type']=='时间'){
                if($data){
                    $builder = $builder->addTime($value['fields'],$value['field_name'],$data[$value['fields']],null,null,true);
                }else{
                    $builder = $builder->addTime($value['fields'],$value['field_name'],null,null,null,true);
                }
            }
            if($value['form_type']=='联动下拉框'){
                if($data){
                    $builder = $builder->addDropDown($value['fields'],$value['field_name'],$data[$value['fields']],$value['table'],$value['hierarchy'],'document/getlianbiao');
                }else{
                    $builder = $builder->addDropDown($value['fields'],$value['field_name'],null,$value['table'],$value['hierarchy'],'document/getlianbiao');
                }
            }
        }
        if($edit){
            $builder = $builder->addFormButton('submit','提交');
        }
        $builder = $builder->addFormButton('back','返回');
        return $builder->fetch();
    }

    /**加载联表数据
     * @param $model_id
     * @param null $id
     * @param string $action
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLianForm($model_id,$id=null,$action='',$pid=null){
        $obj = new Subject();
        $obj->setModelId($model_id);
        $builder = $this->addAction($action.'?model_id='.$model_id);
        $model = $obj->getModel();
        $data = '';
        if($id){
            $data = Db::name($model['model_table'])->where('id',$id)->find();
            $builder = $builder->addNone('id',$id);
        }
//        echo '<pre>';print_r($data);exit;
        $field = $obj->getField();
        foreach ($field as $value){
            if($value['fields']=='pid'){
                $options = Db::name($model['model_table'])->select();
                $options = array_merge([['name'=>'最上级','id'=>0]],$options);
                foreach ($options as $key=>$va){
                    $option[$va['id']] = $va['name'];
                }
//                echo '<pre>';print_r($option);exit;
                if($data){
                    $builder->addSelect('pid','上级',$data['pid'],$option,true);
                }else{
                    $builder->addSelect('pid','上级',$pid,$option,true);
                }
            }else{
                if($value['form_type']=='单行文本'){
                    if($value['fields']=='sort'){
                        if($data){
                            $builder = $builder->addInput($value['fields'],$value['field_name'],$data[$value['fields']]);
                        }else{
                            $builder = $builder->addInput($value['fields'],$value['field_name'],100);
                        }
                    }else{
                        if($data){
                            $builder = $builder->addInput($value['fields'],$value['field_name'],$data[$value['fields']]);
                        }else{
                            $builder = $builder->addInput($value['fields'],$value['field_name']);
                        }
                    }
                }
                if($value['form_type']=='多行文本'){
                    if($data){
                        $builder = $builder->addInput($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addInput($value['fields'],$value['field_name']);
                    }
                }
                if($value['form_type']=='单文件'){
                    if($data){
                        $builder = $builder->addFile($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addFile($value['fields'],$value['field_name']);
                    }
                }
                if($value['form_type']=='多文件'){
                    if($data){
                        $builder = $builder->addFiles($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addFiles($value['fields'],$value['field_name']);
                    }
                }
                if($value['form_type']=='单图片'){
                    if($data){
                        $builder = $builder->addImg($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addImg($value['fields'],$value['field_name']);
                    }
                }
                if($value['form_type']=='多图片'){
                    if($data){
                        $builder = $builder->addImgs($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addImgs($value['fields'],$value['field_name']);
                    }
                }
                if($value['form_type']=='下拉框'){
                    $option = explode(',',trim($value['option'],','));
                    if($data){
                        $builder = $builder->addSelect($value['fields'],$value['field_name'],$data[$value['fields']],$option);
                    }else{
                        $builder = $builder->addSelect($value['fields'],$value['field_name'],null,$option);
                    }
                }
                if($value['form_type']=='单选框'){
                    $option = explode(',',trim($value['option'],','));
                    if($data){
                        $builder = $builder->addRadio($value['fields'],$value['field_name'],$data[$value['fields']],$option);
                    }else{
                        $builder = $builder->addRadio($value['fields'],$value['field_name'],null,$option);
                    }
                }
                if($value['form_type']=='多选框'){
                    $option = explode(',',trim($value['option'],','));
                    if($data){
                        $da = explode(',',trim($data[$value['fields']]));
                        $builder = $builder->addCheckbox($value['fields'],$value['field_name'],$da,$option);
                    }else{
                        $builder = $builder->addCheckbox($value['fields'],$value['field_name'],null,$option);
                    }
                }
                if($value['form_type']=='编辑框'){
                    if($data){
                        $builder = $builder->addEditor($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addEditor($value['fields'],$value['field_name']);
                    }
                }
                if($value['form_type']=='开关'){
                    if($data){
                        $builder = $builder->addSwitch($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addSwitch($value['fields'],$value['field_name']);
                    }
                }
                if($value['form_type']=='时间'){
                    if($data){
                        $builder = $builder->addTime($value['fields'],$value['field_name'],$data[$value['fields']]);
                    }else{
                        $builder = $builder->addTime($value['fields'],$value['field_name']);
                    }
                }
                if($value['form_type']=='联动下拉框'){
                    if($data){
                        $builder = $builder->addDropDown($value['fields'],$value['field_name'],$data[$value['fields']],$value['table'],$value['hierarchy'],'document/getlianbiao');
                    }else{
                        $builder = $builder->addDropDown($value['fields'],$value['field_name'],null,$value['table'],$value['hierarchy'],'document/getlianbiao');
                    }
                }
            }
        }
        $builder = $builder->addFormButton('submit','提交')
            ->addFormButton('back','返回');
        return $builder->fetch();
    }
    /**加载数据
     * @param string $template
     * @param array $vars
     * @param array $config
     * @return mixed
     */
    public function fetch($template = '', $vars = [], $config = [])
    {
        $this->assign('header',$this->header);
//        echo '<pre>';print_r($this->header);exit;
        $this->assign('right_header',$this->right_header);
        $this->assign('column',$this->column);
        $this->assign('columns',json_encode($this->column));
//        echo '<pre>';print_r($this->column);exit;
        $this->assign('right',$this->right);
//        echo '<pre>';print_r($this->page);exit;
        $this->assign('page',$this->page);
        $this->assign('action',$this->action);
//        echo '<pre>';print_r($this->form);exit;
        $this->assign('form',$this->form);
        $this->assign('select',$this->select);
        $this->assign('button',$this->button);
        if(!$template){
            if($this->type=='form'){
                $template = 'public/form';
            }elseif ($this->type=='table'){
                $template = 'public/tables';
            }
        }
//        echo $template;exit;
        return parent::fetch($template, $vars, $config); // TODO: Change the autogenerated stub
    }
}